package test.mapreduce;

import java.io.IOException;
import java.lang.InterruptedException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class CountName {
	static class CountNameMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
		public void map(LongWritable clef, Text valeur, Context contexte) throws IOException, InterruptedException {
			String ligne = valeur.toString();
			String[] split = ligne.split(":");
			contexte.write(new Text(split[2]), new IntWritable(1));
		}
	}
	
	public static class CountNameReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text clef, Iterable<IntWritable> valeurs, Context contexte) throws IOException, InterruptedException {
			int somme = 0;
			for (IntWritable val : valeurs) {
				somme += val.get();
			}
			contexte.write(clef, new IntWritable(somme));
		}
	}
	
	public static void main(String[] args) throws Exception {
		if (args.length != 2) {
			System.err.println(args.length + "(" + args[0] + "," + args[1] + ")");
			System.err.println("Usage : CountName <source> <destination>");
			System.exit(-1);
		}
		
		Job job = new Job();
		job.setJarByClass(CountName.class);
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		
		job.setMapperClass(CountNameMapper.class);
		//job.setCombinerClass (ComptageMotCombiner.class) ;
		job.setReducerClass(CountNameReducer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//job.setNumReduceTasks (2) ;
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
